﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms.Integration;
using wf = System.Windows.Forms;
using io = System.IO;
using System.Diagnostics;

namespace comm.editor
{
    public class FileChooseEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {
                var uc = new UCFileChoose { FileName = value.ToString() };
                var elHost = new ElementHost
                {
                    Width = 120,
                    Height = 40,
                    Child = uc
                };
                edSvc.DropDownControl(elHost);
                return uc.FileName;
            }
            return value;
        }
    }

    /// <summary>
    /// UCFileChoose.xaml 的交互逻辑
    /// </summary>
    public partial class UCFileChoose : UserControl
    {
        public string FileName { get; set; }

        public UCFileChoose()
        {
            InitializeComponent();

            btnChoose.Click += new RoutedEventHandler(btnChoose_Click);
            btnOpen.Click += new RoutedEventHandler(btnOpen_Click);
        }

        void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            Process.Start(FileName.Replace("[ROOT]", DefineFile.LastInstance.Folder));
        }
        void btnChoose_Click(object sender, RoutedEventArgs e)
        {
            var ext = io.Path.GetExtension(FileName);
            var ofd = new wf.OpenFileDialog { Filter = ext + "|*" + ext + "|*.*|*.*" };
            if (ofd.ShowDialog().ToString() == "OK")
            {
                FileName = ofd.FileName;
            }
        }
    }
}
